package com.sanron.sunweather.fragment;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.sanron.sunweather.R;
import com.sanron.sunweather.activities.AddCityActivity;
import com.sanron.sunweather.common.IconManager;
import com.sanron.sunweather.entity.Weather;
import com.sanron.sunweather.entity.WeatherData;
import com.sanron.sunweather.event.SelectCityEvent;

public class MenuFrag extends BaseFragment implements OnClickListener, OnItemClickListener{
	
	private View contentView;
	private Button btnAddCity;
	private Button btnSetting;
	private Button btnRecommand;
	private ListView lvCities;
	private List<String> cities;
	private CityAdapter adapter;
	private Map<String,WeatherData> weatherDatas;
	public static final int REQUEST_ADD_CITY = 0x110;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		initView(inflater);
		setListener();
		return contentView;
	}
	
	public void initView(LayoutInflater inflater){
		contentView = inflater.inflate(R.layout.layout_right_menu, null);
		btnAddCity = (Button)contentView.findViewById(R.id.btn_add_city);
		btnRecommand = (Button)contentView.findViewById(R.id.btn_recommand);
		btnSetting = (Button)contentView.findViewById(R.id.btn_setting);
		lvCities = (ListView)contentView.findViewById(R.id.lv_city);
		adapter =  new CityAdapter();
		lvCities.setAdapter(adapter);
		lvCities.setVerticalScrollBarEnabled(false);
	}
	
	public void setListener(){
		btnAddCity.setOnClickListener(this);
		btnSetting.setOnClickListener(this);
		btnRecommand.setOnClickListener(this);
		lvCities.setOnItemClickListener(this);
	}

	public void setCities(List<String> cities){
		this.cities = cities;
	}
	
	public void setWeatherDatas(Map<String,WeatherData> weatherDatas){
		this.weatherDatas = weatherDatas;
	}

	@Override
	protected void updateViews() {
		adapter.notifyDataSetChanged();
	}
	
	public class CityAdapter extends BaseAdapter{
		
		@Override
		public int getCount() {
			return cities==null ? 0 : cities.size();
		}

		@Override
		public Object getItem(int position) {
			return cities.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			String city = cities.get(position);
			WeatherData weatherData = weatherDatas.get(city);
			if(convertView==null){
				convertView = LayoutInflater.from(getActivity()).inflate(R.layout.list_item_city, null);
			}
			TextView tvCity = (TextView) convertView.findViewById(R.id.tv_city);
			TextView tvTemp = (TextView) convertView.findViewById(R.id.tv_temp);
			TextView tvType = (TextView) convertView.findViewById(R.id.tv_weather_type);
			ImageView ivIcon = (ImageView) convertView.findViewById(R.id.iv_weather_icon);
			tvCity.setText(city);
			
			if(position==0){
				tvCity.setCompoundDrawablePadding(10);
				tvCity.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_home_city, 0, 0, 0);
			}else{
				tvCity.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0);
			}
			
			if(weatherData!=null){
				Weather weather = weatherData.getWeathers().get(0);
				tvTemp.setText(weather.getHighTEMP()+"°~"+weather.getLowTEMP()+"°");
				tvType.setText(weather.getDayType());
				int iconId = IconManager.getTypeSmallIcon(weather.getDayType(),true);
				ivIcon.setImageResource(iconId);
			}else{
				tvTemp.setText("");
				tvType.setText("");
				ivIcon.setImageBitmap(null);
			}

			int size = getResources().getDimensionPixelSize(R.dimen.menu_button_size);
			LayoutParams lp = new LayoutParams(size, size);
			convertView.setLayoutParams(lp);
			
			return convertView;
		}
		
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_add_city:{
			Intent intent = new Intent(getActivity(),AddCityActivity.class);
			getActivity().startActivityForResult(intent, REQUEST_ADD_CITY);
		}break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		if(weatherDatas.get(cities.get(position))!=null){
			//有数据才可以切换
			eventBus.post(new SelectCityEvent(position));
		}
	}
	
}
